/*
 * @Author: your name
 * @Date: 2021-12-21 10:28:30
 * @LastEditTime: 2021-12-21 16:56:40
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \day10 react\my-app\src\store\index.js
 */
import {createStore,applyMiddleware} from "redux"
import logger from "redux-logger"
import thunk from "redux-thunk"

const reducer=(state={home:[],classify:[],newList:[]},action)=>{
    switch (action.type) {
        case "HOME":
            state.home=action.home
            return {
                ...state,
                home:[...state.home],
                classify:[...state.classify]
            }
        case "CLASSIFY":
            state.classify=action.classify
            return {
                ...state,
                home:[...state.home],
                classify:[...state.classify]
            }
        // case "CHANGECHECK":
        //     state.classify.forEach(item=>{
        //         if(item.id===action.payload){
        //             item.check=!item.check
        //         }
        //     })
        //     return {
        //         ...state,
        //         home:[...state.home],
        //         classify:[...state.classify]
        //     }
        case "CHANGESTART":
            console.log(action);
            let ind=state.classify.findIndex((item) => item.id===action.payload.id)
            state.classify[ind]=action.payload
            return {
                ...state,
                home:[...state.home],
                classify:[...state.classify]
            }
        case "CHANGECOLL":
            state.newList=action.payload
            return {
                ...state,
                home:[...state.home],
                classify:[...state.classify],
                newList:[...state.newList]
            }
        default:
            return {
                ...state,
                home:[...state.home],
                classify:[...state.classify]
            }
    }
}

const store=createStore(reducer,applyMiddleware(thunk,logger))

export default store